/**
 * @FileDescription:
 * @Author: kahu
 * @Date: 2022/11/3
 * @LastEditors: kahu
 * @LastEditTime: 2022/11/3
 */
/**
 * 跳转非tabbar页面
 * @param url 路径
 * @param param 参数对象
 * @param timeout 延时时间，默认0
 * @param type default普通跳转 redirect关闭当前页面跳转  reLaunch关闭所有页面跳转 默认default
 */
export const jump = (url, param, timeout = 0, type = "default") => {
	setTimeout(() => {
		if (param) {
			url = `${url}?detail=${encodeURIComponent(JSON.stringify(param))}`
		}
		if (type === 'default') {
			uni.navigateTo({
				url
			})
		} else if (type === 'redirect') {
			uni.redirectTo({
				url
			})
		} else {
			uni.reLaunch({
				url
			})
		}
	}, timeout)
}

/**
 * 跳转到tabbar页面
 * @param url 路径
 * @param timeout 延时时间，默认0
 */
export const jumpToTabbar = (url, timeout = 0) => {
	setTimeout(() => {
		uni.switchTab({
			url
		})
	}, timeout)
}

/**
 * 关闭当前页面，返回上一级或者多级页面
 * @param delta 级数，默认1
 */
export const goBack = (delta = 1) => {
	uni.navigateBack({
		delta
	})
}

/**
 * 获取跳转参数
 * @param loadParam
 * @return {{}|any}
 */
export const getJumpParam = (loadParam) => {
	if (typeof loadParam === "object" && loadParam?.detail) {
		let data = loadParam.detail
		// #ifdef H5
		if (data.indexOf('%') > -1) { // 如果url中存在 '%'
			data = data.replace(/%/g, '%25'); // 进行替换操作
		}
		// #endif
		return JSON.parse(decodeURIComponent(data))
	}
	return {}
}

export const goExternalLink = (url, parm) => {
	if (url.includes('https://') || url.includes('http://')) {} else {
		url = 'http://' + url;
	}
	// #ifdef H5
	window.location.href = url
	console.log('go:' + url)
	// #endif
	// #ifdef APP-PLUS
	plus.runtime.openURL(url, function(e) {
		console.log(e);
	})
	// #endif
	console.log('go:' + url)
	// #ifdef MP-WEIXIN|MP-ALIPAY
	let array = url.split('?')
	url = array[0]
	let data = array[1] ? encodeURIComponent(array[1]) : ''
	console.log('url:' + url + ',data:' + data)
	url = `../../../pages_category_page1/linkOthers/index?url=${url}&data=${data}`
	console.log('go:' + url)
	uni.navigateTo({
		url: url
	})
	// #endif
}

/**
 * 从url中得到路径参数
 */
export const getUrlQueryFromParm = (str) => {
	let url = decodeURIComponent(str)
	console.log("getUrlQueryFromParm url：" + url)
	let i = url.indexOf('?')
	if (i < 0 || i === url.length - 1) return {}
	let arr = url.split('?')[1].split('&')
	console.log("getUrlQueryFromParm arr：" + JSON.stringify(arr))
	let obj = {}
	arr.forEach(item => {
		let brr = item.split('=')
		obj[brr[0]] = brr.length > 1 ? brr[1] : null
	})
	console.log("getUrlQueryFromParm obj：" + JSON.stringify(obj))
	return obj
}